package com.wutong.authority_server.security;

import com.alibaba.fastjson.JSONObject;
import com.wutong.authority_server.entity.ResultCode;
import com.wutong.common_server.result.Response;
import com.wutong.common_server.utils.ResponseUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//解决认证过的用户访问无权限资源时的异常处理器
public class CustomAccessDeineHandler implements AccessDeniedHandler {


    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        Response failure = ResponseUtil.failure(ResultCode.NO_PERMISSION.getMessage(),ResultCode.NO_PERMISSION.getCode());
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/javascript;charset=utf-8");
        response.getWriter().print(JSONObject.toJSONString(failure));
    }

}
